<?php

namespace Lib\Core;

/**
 * mailhandler.php
 *
 * Created on 2012-2-29
 * Create by Calvin.Wang
 */
include __DIR__ . '/PHPMailer/' . 'PHPMailerAutoload.php';

class Mail extends \PHPMailer {

	private $mailModel;

	private static $tn = PHP_EOL;

	
	public function __construct($item = 'default') {
		if (is_array($item)) {
			$config = $item;
		} else {
			$config = (array) new \Config\Mail();
			$config = $config[$item];
		}
		$this->CharSet = 'UTF-8';
		$this->Host = $config['host'];
		$this->Helo = $config['host'];
		$this->Port = $config['port'];
		$this->SMTPAuth = false;
		if (!empty($config['auth'])) {
			$this->Username = $config['username'];
			$this->Password = $config['password'];
			$this->SMTPAuth = true;
		}
		$this->IsSMTP();
		parent::__construct(true);
	}

	public function sendMail($to, $subject, $content, $attache = array()) {
		$from = $this->Username;
		$this->SetFrom($from);
		$mailTo = is_array($to) ? $to : array($to);
		$this->MsgHTML($content);
		$this->Subject = $subject;
		foreach($mailTo as $k => $v) {
			$this->AddAddress($v, $v);
		}
		if(count($attache) > 0) {
			foreach($attache as $k => $AttachmentAddress) {
				$this->AddAttachment($AttachmentAddress);
			}
		}
		if(!$this->Send()) {
			throw new MailExceptionLib($this->ErrorInfo);
		}
	}
}